
package com.cn.ycy.controller;

import com.cn.ycy.dto.ReturnT;
import com.cn.ycy.dto.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.util.HashMap;
import java.util.Map;

/**
 * 演示 spring推荐的 模版引擎
 */
@Controller
@RequestMapping("user")
public class RedictAndforwardController {

    static Logger logger = LoggerFactory.getLogger(CookieController.class);

    //---------资源访问问题---------

    /**
     * 跳转到 userpage.html 的实现,有一下三种
     */
    @RequestMapping()
    public ModelAndView sayHello() {
        return new ModelAndView("/userpage.html");
    }

    /**
     * 指定了 html
     */
    @RequestMapping("/userpage1")
    public String sayHello1() {
        return "/userpage.html";
    }

    /**
     * 不指定 html
     */
    @RequestMapping("/userpage2")
    public String sayHello2(Model model) {
        putModel(model);
        return "/userpage";
    }

    /**
     * 不指定 html
     */
    @RequestMapping("/login")
    public String login(Model model) {
        putModel(model);
        return "/login";
    }

    private void putModel(Model model) {
        model.addAttribute("name", "ycy");
    }

    //---------重定向---------
    @RequestMapping("/baidu")
    public String redirectBaidu() {
        return "redirect:https://www.baidu.com?code=1";
    }

    //这个重定向是相对路径，所以跳转不到 www.baidu.com
    @RequestMapping("/baidu1")
    public RedirectView redirectBaidu1() {
        return new RedirectView("www.baidu.com");
    }

    //---------转发---------
    @RequestMapping("/user")
    public String sayHelloForward() {
        return "forward:/user";
    }

    @RequestMapping(value = "/loadmin", method = RequestMethod.GET)
    @ResponseBody
    public ReturnT<User> load() {
        logger.info("userId {}", -1);
        User user = new User();
        user.setUsername("user");
        user.setUserId(-1);
        return new ReturnT<User>(user);
    }

}
